package com.algorithm.pack;

import org.junit.Before;
import org.junit.Test;

public class TestCompletePack {
    int[] weight;
    int[] value;
    int volume;
    int N;

    @Before
    public void initData(){
        weight = new int[]{5,4,7,2,6};
        value = new int[]{12,3,10,3,6};
        volume = 16;
        N = 4;
    }

    @Test
    public void testCompletePack_recursive(){
        int max_value = CompletePack.completePack_recursive(N,weight,value,volume);

        System.out.println("The max value is:"+max_value);

        int remain_space = volume;
        int count;

        for(int i=N;i>=1;i--){
            count = 0;
            while (remain_space>=weight[i-1]){
                int value1 = CompletePack.completePack_recursive(i,weight,value,remain_space);
                int value2 = CompletePack.completePack_recursive(i,weight,value,remain_space-weight[i-1]);
                if(value1-value2==value[i-1]){
                    count++;
                    remain_space -= weight[i-1];
                }else{
                    break;
                }
            }
            if(count>0){
                System.out.println("The item "+i+" selected count is "+count+".");
            }
        }
    }

    @Test
    public void testCompletePack_space(){
        int F[] = CompletePack.completePack_space(weight,value,volume);

        Tools.printArray(F);

        System.out.println("The max value is:"+F[volume]);
    }

    @Test
    public void testCompletePack(){
        int F[][] = CompletePack.completePack(weight,value,volume);

        Tools.printArray(F);

        System.out.println("The max value is:"+F[N][volume]);

        int remain_space = volume;
        int count;

        for (int i=N;i>=1;i--){
            count = 0;
            while(remain_space>=weight[i-1]){
                if(F[i][remain_space]-F[i][remain_space-weight[i-1]]==value[i-1]){
                    count++;
                    remain_space -= weight[i-1];
                }else{
                    break;
                }
            }
            if(count>0){
                System.out.println("The item "+i+" selected count is "+count+".");
            }
        }
    }
}
